package lcc.cj;




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import lcc.cj.product;
import lcc.cj.ConnectionManager;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import org.w3c.dom.Node;





public class CJParser {

	
	public static void main(String[] args) throws ClientProtocolException, IOException, URISyntaxException, SAXException, ParserConfigurationException, XPathExpressionException {
		
		URI url =new URI ("https://product-search.api.cj.com/v2/product-search?website-id=2054196");
		String key="0085e53d208e50f62e676ec1290fbd5c96cf51f1d3d0b28083fe74ce5c771c37e281993add59d3e6c5041e61cdaa52889b0cab2ef4cf9d15e1f2861cf9325b9e6d/50cc7102c05ee2f045c27415cf45ba25d79ddf8cae97c92dddd3a6e3ed8cbdbbacbcf584715103d0fd1043d4b535d89edd216d2be23c9b7df541bccfc4b3b511";
		
		System.out.println("Connecting........");
				
		
	
		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(url);
		request.addHeader("authorization",key);
		HttpResponse response = client.execute(request);			
		System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
		
		
		
        
		
		
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder dBuilder;
	     
	        try {
	            dBuilder = dbFactory.newDocumentBuilder();
	            Document doc = dBuilder.parse(response.getEntity().getContent());            
	            doc.getDocumentElement().normalize();
	            
	            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
	            NodeList nodeList = doc.getElementsByTagName("product");
	            System.out.println("----------------------");
	            
	            
	            List<product> productList = new ArrayList<product>();
	            for (int temp = 0; temp < nodeList.getLength(); temp++) {
	            	 
	        		Node nNode = nodeList.item(temp);
	        		     
	        			        		
	        		for(product Product : productList) {
	        			ConnectionManager.insertCouponFeed(Product);
	        			
	        		    //System.out.println(Product);
	        		}
	         
	        		if (nNode.getNodeType() == Node.ELEMENT_NODE) {
	         
	        			Element eElement = (Element) nNode;
	        			product Product=new product();
	        			
	        			Product.setAdid(eElement.getElementsByTagName("ad-id").item(0).getTextContent());
	        			Product.setAdvertiserid(eElement.getElementsByTagName("advertiser-id").item(0).getTextContent());
	        			Product.setAdvertisername(eElement.getElementsByTagName("advertiser-name").item(0).getTextContent());
	        			Product.setAdvertisercategory(eElement.getElementsByTagName("advertiser-category").item(0).getTextContent());
	        			Product.setBuyurl(eElement.getElementsByTagName("buy-url").item(0).getTextContent());
	        			Product.setCatalogid(eElement.getElementsByTagName("catalog-id").item(0).getTextContent());
	        			Product.setCurrency(eElement.getElementsByTagName("currency").item(0).getTextContent());
	        			Product.setDescription(eElement.getElementsByTagName("description").item(0).getTextContent());
	        			Product.setImageurl(eElement.getElementsByTagName("image-url").item(0).getTextContent());
	        			Product.setInstock(eElement.getElementsByTagName("in-stock").item(0).getTextContent());
	        			Product.setIsbn(eElement.getElementsByTagName("isbn").item(0).getTextContent());
	        			Product.setManufacturername(eElement.getElementsByTagName("manufacturer-name").item(0).getTextContent());
	        			Product.setManufacturersku(eElement.getElementsByTagName("manufacturer-sku").item(0).getTextContent());
	        			Product.setName(eElement.getElementsByTagName("name").item(0).getTextContent());
	        			Product.setPrice(eElement.getElementsByTagName("price").item(0).getTextContent());
	        			Product.setRetailprice(eElement.getElementsByTagName("retail-price").item(0).getTextContent());
	        			Product.setSaleprice(eElement.getElementsByTagName("sale-price").item(0).getTextContent());
	        			Product.setSku(eElement.getElementsByTagName("sku").item(0).getTextContent());
	        			Product.setUpc(eElement.getElementsByTagName("upc").item(0).getTextContent());
	                    productList.add(Product);
	                    
	                    
	                    
	                    
	        		/*	
	        			System.out.println("Ad:id : " + eElement.getElementsByTagName("ad-id").item(0).getTextContent());
	        			System.out.println("advertiser-id : " + eElement.getElementsByTagName("advertiser-id").item(0).getTextContent());
	        			System.out.println("advertiser-name: " + eElement.getElementsByTagName("advertiser-name").item(0).getTextContent());
	        			System.out.println("advertiser-category : " + eElement.getElementsByTagName("advertiser-category").item(0).getTextContent());
	        			System.out.println("buy-url : " + eElement.getElementsByTagName("buy-url").item(0).getTextContent());
	        			System.out.println("catalog-id : " + eElement.getElementsByTagName("catalog-id").item(0).getTextContent());
	        			System.out.println("currency : " + eElement.getElementsByTagName("currency").item(0).getTextContent());
	        			System.out.println("description : " + eElement.getElementsByTagName("description").item(0).getTextContent());
	        			System.out.println("image-url : " + eElement.getElementsByTagName("image-url").item(0).getTextContent());
	        			System.out.println("in-stock : " + eElement.getElementsByTagName("in-stock").item(0).getTextContent());
	        			System.out.println("isbn : " + eElement.getElementsByTagName("isbn").item(0).getTextContent());
	        			System.out.println("manufacturer-name: " + eElement.getElementsByTagName("manufacturer-name").item(0).getTextContent());
	        			System.out.println("manufacturer-sku : " + eElement.getElementsByTagName("manufacturer-sku").item(0).getTextContent());
	        			System.out.println("name : " + eElement.getElementsByTagName("name").item(0).getTextContent());
	        			System.out.println("price : " + eElement.getElementsByTagName("price").item(0).getTextContent());
	        			System.out.println("retail-price : " + eElement.getElementsByTagName("retail-price").item(0).getTextContent());
	        			System.out.println("sale-price : " + eElement.getElementsByTagName("sale-price").item(0).getTextContent());
	        			System.out.println("sku : " + eElement.getElementsByTagName("sku").item(0).getTextContent());
	        			System.out.println("upc : " + eElement.getElementsByTagName("upc").item(0).getTextContent());
	        			System.out.println("______________________________");
	        		*/	
	         
	        		}
	        		
	        		
	        	  }
	            System.out.println("Successfully inserted into DB");
	            System.out.println("Inserting data into DB");
	            } catch (Exception e) {
	        	e.printStackTrace();
	            }
	            
	   }
	
}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		